package com.tn.service.task.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tn.entity.TaskInfoLocal;
import com.tn.mapper.TaskInfoLocalMapper;
import com.tn.service.task.TaskInfoLocalService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;


import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author YuzSuzuki
 * @since 2023-04-03
 */
@Service
public class TaskInfoLocalServiceImpl extends ServiceImpl<TaskInfoLocalMapper, TaskInfoLocal> implements TaskInfoLocalService {


    @Override
    public void insert(TaskInfoLocal taskInfoLocal) {
        QueryWrapper<TaskInfoLocal>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("task_no",taskInfoLocal.getTaskNo());
        TaskInfoLocal taskInfoLocal1 = baseMapper.selectOne(queryWrapper);
        if(taskInfoLocal1!=null){
            throw new RuntimeException("已存在相同任务,新增失败");
        }
        baseMapper.insert(taskInfoLocal);
    }

    @Override
    public void update(TaskInfoLocal taskInfoLocal) {
        QueryWrapper<TaskInfoLocal>queryWrapper=new QueryWrapper<>();
        queryWrapper.ne("id",taskInfoLocal.getId());
        queryWrapper.eq("task_no",taskInfoLocal.getTaskNo());
        TaskInfoLocal taskInfoLocal1 = baseMapper.selectOne(queryWrapper);
        if(taskInfoLocal1!=null){
            throw new RuntimeException("已存在相同任务，编辑失败");
        }
        baseMapper.insert(taskInfoLocal);
    }

    @Override
    public void deleteByIds(List<Long> ids) {
        baseMapper.deleteBatchIds(ids);
    }

    @Override
    public PageInfo<TaskInfoLocal> list(TaskInfoLocal taskInfoLocal) {
        PageHelper.startPage(taskInfoLocal.getPageNum(),taskInfoLocal.getPageSize());
        QueryWrapper<TaskInfoLocal>wrapper=new QueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(taskInfoLocal.getTaskNo()),"task_no",taskInfoLocal.getTaskNo());
        wrapper.eq(StringUtils.isNotBlank(taskInfoLocal.getGoodsName()),"goods_name",taskInfoLocal.getGoodsName());
        wrapper.eq(StringUtils.isNotBlank(taskInfoLocal.getWorkProcessName()),"work_process_name",taskInfoLocal.getWorkProcessName());
        wrapper.eq(StringUtils.isNotBlank(taskInfoLocal.getShipNo()),"ship_no",taskInfoLocal.getShipNo());
        wrapper.eq(StringUtils.isNotBlank(taskInfoLocal.getConsignCompanyName()),"consign_company_name",taskInfoLocal.getConsignCompanyName());
        wrapper.eq(StringUtils.isNotBlank(taskInfoLocal.getCustomerCompanyName()),"customer_company_name",taskInfoLocal.getCustomerCompanyName());
        wrapper.eq(StringUtils.isNotBlank(taskInfoLocal.getTakeWay()),"take_way",taskInfoLocal.getTakeWay());
        wrapper.eq(StringUtils.isNotBlank(taskInfoLocal.getWeighMethod()),"weigh_method",taskInfoLocal.getWeighMethod());
        List<TaskInfoLocal> taskInfoLocals = baseMapper.selectList(wrapper);
        PageInfo<TaskInfoLocal> taskInfoLocalPageInfo = new PageInfo<>(taskInfoLocals);
        return taskInfoLocalPageInfo;
    }

    @Override
    public TaskInfoLocal selectById(Long id) {
        return baseMapper.selectById(id);
    }
}
